<?php

namespace app\money\controller;

use app\admin\common\Base;
use app\admin\controller\Store;
use think\Db;
// 利润表
class Profitdetail extends Base
{
    public function index()
    {
        $data = $this->request->param();

        if (isset($data['ajax']) && $data['ajax'] == 100) {
            $map = '';
            $page = isset($data['page']) ? $data['page'] : 1;
            $page = intval($page);
            $limit = isset($data['rows']) ? $data['rows'] : 10;
            $limit = intval($limit);
            $start = $limit * ($page - 1);

            $list0 = Db::name('order')->where('big_type', 0)->where('dj_time', '>', strtotime($data['startdate']))->where('dj_time', '<', strtotime($data['enddate']))->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->order('addtime desc,id asc')->select();

            $list1 = Db::name('order')->where('big_type', 1)->where('dj_time', '>', strtotime($data['startdate']))->where('dj_time', '<', strtotime($data['enddate']))->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->order('addtime desc,id asc')->select();

            // 收入
            $list0_saler_user_price = round(array_sum(array_column($list0, 'payment')), 2);
            // halt($list0_saler_user_price);
            $list0_yh_e = array_sum(array_column($list0, 'yh_e'));
            $list0_yh_lv = count($list0) != 0 ? round(array_sum(array_column($list0, 'yh_lv')) / count($list0), 2)  . '%' : '';

            // 成本
            $list1_saler_user_price = round(array_sum(array_column($list1, 'payment')), 2);
            $list1_yh_e = array_sum(array_column($list1, 'yh_e'));
            $list1_yh_lv = count($list1) != 0 ? round(array_sum(array_column($list1, 'yh_lv')) / count($list1), 2)  . '%' : '';

            // 利润
            $rl = round($list0_saler_user_price / 1  - $list1_saler_user_price / 1, 2);

            // 其他收支
            $qtrs = Db::name('income')->where('type', 0)->where('dj_time', '>', strtotime($data['startdate']))->where('dj_time', '<', strtotime($data['enddate']))->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->order('addtime desc,id asc')->select();
            $qtzc = Db::name('income')->where('type', 1)->where('dj_time', '>', strtotime($data['startdate']))->where('dj_time', '<', strtotime($data['enddate']))->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->order('addtime desc,id asc')->select();
            // halt($saler_user_price);

            $rs = array_sum(array_column($qtrs, 'now_jine'));
            $zc = array_sum(array_column($qtzc, 'now_jine'));

            $qtrl = round($rs / 1 - $zc / 1, 2);

            $arr = [];
            array_push($arr, ['project' => '主营业务', 'row' => 1, 'price' => '']);
            array_push($arr, ['project' => ' 销售收入', 'row' => 2, 'price' => $list0_saler_user_price]);
            array_push($arr, ['project' => ' 减：销售优惠金额', 'row' => 3, 'price' => $list0_yh_e]);
            array_push($arr, ['project' => ' 减：收款折扣', 'row' => 4, 'price' => $list0_yh_lv]);

            array_push($arr, ['project' => '', 'row' => 5, 'price' => '']);

            array_push($arr, ['project' => ' 销售成本', 'row' => 6, 'price' => $list1_saler_user_price]);
            array_push($arr, ['project' => ' 减：购货优惠金额', 'row' => 7, 'price' => $list1_yh_e]);
            array_push($arr, ['project' => ' 减：付款折扣', 'row' => 8, 'price' => $list1_yh_lv]);

            array_push($arr, ['project' => '', 'row' => 9, 'price' => '']);

            array_push($arr, ['project' => ' 盘盈盘亏', 'row' => 10, 'price' => '']);

            array_push($arr, ['project' => '', 'row' => 11, 'price' => '']);

            array_push($arr, ['project' => '主营业务利润', 'row' => 12, 'price' => $rl]);

            array_push($arr, ['project' => '', 'row' => 13, 'price' => '']);

            array_push($arr, ['project' => '其他业务', 'row' => 14, 'price' => '']);
            array_push($arr, ['project' => ' 其他收入', 'row' => 15, 'price' => $rs]);

            $income_type = Db::name('income_type')->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->select();
            $income = Db::name('income')->alias('a')->join('income_good b', 'a.id = b.pid')->where('a.dj_time', '>', strtotime($data['startdate']))->where('a.dj_time', '<', strtotime($data['enddate']))->where('b.type', 0)->where(['a.is_del' => 0, 'a.enterprise_id' => $this->EnterpriseId])->where(['b.is_del' => 0, 'b.enterprise_id' => $this->EnterpriseId])->select();
            $row = 16;
            // halt($income_type);
            foreach ($income_type as $key => $value) {
                $price = 0;
                foreach ($income as $k => $v) {
                    if ($v['income_type_id'] == $value['id']) {
                        $price += $v['price'];
                    }
                }
                array_push($arr, ['project' => '  其中：' . $value['name'], 'row' => $row + $key, 'price' => $price]);
            }

            array_push($arr, ['project' => ' 其它支出', 'row' => $row + count($income_type), 'price' => $zc]);

            $income_zc = Db::name('income')->alias('a')->join('income_good b', 'a.id = b.pid')->where('a.dj_time', '>', strtotime($data['startdate']))->where('a.dj_time', '<', strtotime($data['enddate']))->where('b.type', 0)->where(['a.is_del' => 0, 'a.enterprise_id' => $this->EnterpriseId])->where(['b.is_del' => 0, 'b.enterprise_id' => $this->EnterpriseId])->select();

            $row_1 = $row + count($income_type) + 1;
            foreach ($income_zc as $key => $value) {
                $price = 0;
                foreach ($income as $k => $v) {
                    if ($v['income_type_id'] == $value['id']) {
                        $price += $v['price'];
                    }
                }
                $name = isset($value['name']) ? '  其中：' . $value['name'] : '';
                array_push($arr, ['project' => $name, 'row' => $row_1 + $key, 'price' => $price]);
            }

            array_push($arr, ['project' => '', 'row' => $row_1 + count($income_zc) + 1, 'price' => '']);

            array_push($arr, ['project' => '其他业务利润', 'row' => $row_1 + count($income_zc) + 2, 'price' => $qtrl, 'total' => $rl + $qtrl]);
            // halt($arr);
            return $this->grid_data($arr, count($arr), $page, $limit);
        }

        $month = $this->get_month_start_to_end();
        $this->assign('month', $month);

        return $this->fetch();
    }
}
